package com.doit.demo01;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
@SuppressWarnings("all")
public class cli_ser_TCP {
    public static void main(String[] args) throws IOException {
        method();
    }
    public static void method() throws IOException {
        Socket client = new Socket("localhost", 8080);
        OutputStream os = client.getOutputStream();
        os.write("雅斯雷".getBytes());

        InputStream is = client.getInputStream();
        byte[] bytes = new byte[1024];
        int len =is.read(bytes);
        System.out.println(new String(bytes,0,len));
        is.close();
        os.close();
        client.close();
    }

    public static void method2() throws IOException {
        Socket socket = new Socket("192.168.2.191", 9527);
        new Thread(new Runnable() {
            @Override
            public void run() { //写信息
                while (true){
                    try {
                        OutputStream out =socket.getOutputStream();
                        out.write(new Scanner(System.in).next().getBytes());
                    }catch (IOException e){}
                }
            }
        }).start();

        new Thread(()->{
            try {
                ServerSocket serverSocket1 = new ServerSocket(1910);
                Socket accept1 = serverSocket1.accept();
                while (true) {
                    InputStream in = accept1.getInputStream();
                    byte [] bytes = new byte[1024];
                    int len = in.read(bytes);
                    System.out.println(new String(bytes,0,len));
                    in.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }).start();
    }

    public static void method3() throws IOException {
        Socket socket = new Socket("192.168.2.145", 10085);
        new Thread(new Runnable() {
            @Override
            public void run() { //写信息
                while (true){
                    try {
                    OutputStream out  = socket.getOutputStream();

                     out.write(new Scanner(System.in).next().getBytes());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();

        new Thread(new Runnable() { //读信息
            @Override
            public void run() {
                while (true){
                    try {
                    InputStream in = socket.getInputStream();
                    byte[] bytes = new byte[1024];
                    int len = in.read(bytes);
                    System.out.println(new String(bytes,0,len));
                    } catch (IOException e) {}
                }
            }
        }).start();


//        out.close();
//        in.close();
//        socket.close();
    }
}
